<template>
  <div class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="true">
        <date-range-picker  class="date-item" v-model="query.createTime"></date-range-picker>

        <el-select
          v-model="query.alarmStatus"
          clearable
          filterable
          placeholder="请选择状态"
          style="width: 100px;"
          class="filter-item"
          @change="crud.toQuery"
        >
          <el-option
            v-for="item in dict.alarm_status"
            :key="item.id"
            :label="item.label"
            :value="item.value"
          />
        </el-select>

        <rrOperation :crud="crud" />
      </div>

      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;">
        <el-table-column prop="alarmStatus" label="状态代码"  align="center">
          <template slot-scope="scope">
            <el-tag :type="getStatusColor(scope.row)">
              {{ dict.label.alarm_status[scope.row.alarmStatus] }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="alarmMsg" label="故障信息" align="center"/>
        <el-table-column prop="alarmCode" label="报警代码" align="center"/>
        <el-table-column prop="startTime" label="发生时间" align="center" width="150px"/>
        <el-table-column prop="endTime" label="解除时间" align="center" width="150px"/>
        <el-table-column prop="tripTime" label="耗时（分）" align="center" width="100px"/>
        <el-table-column prop="createBy" label="操作人" align="center"width="80px"/>
      </el-table>
      <!--分页组件-->
      <pagination />
    </div>
  </div>
</template>

<script>
import crudDeviceAlarm from '@/api/device/deviceAlarm'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import DateRangePicker from '@/components/DateRangePicker'

const defaultForm = { id: null, deviceCode: null, deviceName: null, lineCode: null, sectionCode: null, stationCode: null, alarmStatus: null, alarmMsg: null, alarmCode: null, startTime: null, endTime: null, tripTime: null, createBy: null, createTime: null, updateBy: null, updateTime: null }
export default {
  name: 'DeviceAlarm',
  components: { pagination, crudOperation, rrOperation, udOperation,DateRangePicker },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  dicts: ['alarm_status'],
  cruds() {
    return CRUD({ title: '设备报警记录', url: 'api/deviceAlarm', idField: 'id', sort: 'id,desc',
      optShow: {
        reset: false
      },
      queryOnPresenterCreated: false,
      crudMethod: { ...crudDeviceAlarm }})
  },
  data() {
    return {
      deviceCode: 'TT',
      permission: {
        add: ['admin', 'deviceAlarm:add'],
        edit: ['admin', 'deviceAlarm:edit'],
        del: ['admin', 'deviceAlarm:del']
      },
      rules: {
        deviceCode: [
          { required: true, message: '设备编码不能为空', trigger: 'blur' }
        ],
        deviceName: [
          { required: true, message: '设备名称不能为空', trigger: 'blur' }
        ],
        lineCode: [
          { required: true, message: '产线编码不能为空', trigger: 'blur' }
        ],
        sectionCode: [
          { required: true, message: '工段编码不能为空', trigger: 'blur' }
        ],
        stationCode: [
          { required: true, message: '工位代码不能为空', trigger: 'blur' }
        ],
        alarmStatus: [
          { required: true, message: '状态代码不能为空', trigger: 'blur' }
        ]
      },
      queryTypeOptions: [
        { key: 'deviceCode', display_name: '设备编码' },
        { key: 'deviceName', display_name: '设备名称' },
        { key: 'lineCode', display_name: '产线编码' },
        { key: 'sectionCode', display_name: '工段编码' },
        { key: 'stationCode', display_name: '工位代码' }
      ]
    }
  },
  methods: {
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.params.deviceCode = this.deviceCode;
      return true
    },
    queryPage(code){
      this.deviceCode = code;
      this.crud.toQuery();
    },
    getStatusColor(item){
      if(item.alarmStatus == '0' ){
        return 'success';
      }else if(item.alarmStatus == '1'){
        return 'danger';
      }
    }
  }
}
</script>

<style scoped>

</style>
